﻿using System;
using BusinessLogic.ClassFactory;
using BusinessLogic.BusinessRules;
using Common;
using IssueCommon;

namespace RichECMS.ServerPages
{
    public partial class Top : Util.BasePage.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitDataBind();
                if (State.LoginUser.ManagerCompetence <= (int)Utility.ManagerCompetence.UserCanIssueAndClients)
                {
                    this.LbtnIssueAll.Visible = true;
                }
                else
                {
                    this.LbtnIssueAll.Visible = false;
                }
            }
        }

        /// <summary>
        /// 绑定
        /// </summary>
        private void InitDataBind()
        {
            try
            {
                LiteralLoginUser.Text = State.LoginUser.LoginName;
                using (SectionsFactory objFactory = new SectionsFactory())
                {
                    BusinessLogic.BusinessRules.Sections objsection = objFactory.NewInstance();
                    //读取权限
                    if (State.LoginUser.ManagerCompetence < (int)Utility.ManagerCompetence.UserCanIssueAndClients)
                    {
                        objsection = objFactory.CreatorBySql("intParentID =0 and intWSID=" + State.intWSID + " order by intSortNo desc,  dtmInTime desc ");
                    }
                    else
                    {
                        if (State.LoginUser.Permissions != "")
                        {
                            string strfilter = " and intSectionID in (" + State.LoginUser.Permissions + ") ";
                            objsection = objFactory.CreatorBySql("intParentID =0 and intWSID=" + State.intWSID + strfilter + "  order by intSortNo desc,  dtmInTime desc ");
                        }
                        else
                        {
                            Alert("您没有可管理的栏目，请联系网站管理员");
                        }
                    }

                    if (objsection != null)
                    {
                        rptList.DataSource = objsection.Data.Sections.DataSet.Tables[0].DefaultView;
                        rptList.DataBind();
                    }
                    else
                    {
                        Alert("暂无数据！");
                    }
                }

                using (UserWebRelationFactory objUserWebRelationFactory = new UserWebRelationFactory())
                {
                    UserWebRelation objUserWebRelation = objUserWebRelationFactory.CreatorBySql("chvLoginName='" + State.LoginUser.LoginName + "' and intState=1 and intUserID=" + State.LoginUser.UserID);
                    if (objUserWebRelation != null)
                    {
                        if (objUserWebRelation.Count > 1)
                        {
                            navWebControllink.Text = "<li><a href='MainWebSiteContaolPanel.aspx' title='网站管理' class='changeWindows'>网站管理</a></li>";
                        }
                        else
                        {
                            navWebControllink.Visible = false;
                        }
                    }
                    else
                    {
                        navWebControllink.Visible = false;
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString());
            }
        }

        protected void IssueNew(object sender, EventArgs e)
        {
            using (IssueManage objIssueManage = new IssueManage(State.LoginUser))
            {
                objIssueManage.SessionUser = State.LoginUser;
                if (!AppConfig.GetEnabledInculded())
                {
                    if (!AppConfig.GetCommonIssue())
                    {
                        //不生成common
                    }
                    else
                    {
                        objIssueManage.IssueInclude(State.intWSID);
                    }
                }
                else
                {
                    objIssueManage.IssueInclude(State.intWSID);
                }

                objIssueManage.IssueIndex(State.intWSID);
                //栏目--增量发布
                objIssueManage.IssueSection(false, State.intWSID);

                //文章--增量发布
                objIssueManage.IssueArticle(false, State.intWSID);

                objIssueManage.ClearCaches();
            }
            Alert("增量发布成功！");
        }

        protected void IssueAll(object sender, EventArgs e)
        {
            using (IssueManage objIssueManage = new IssueManage(State.LoginUser))
            {
                objIssueManage.SessionUser = State.LoginUser;
                if (!AppConfig.GetEnabledInculded())
                {
                    if (!AppConfig.GetCommonIssue())
                    {
                        //不生成common
                    }
                    else
                    {
                        objIssueManage.IssueInclude(State.intWSID);
                    }
                }
                else
                {
                    objIssueManage.IssueInclude(State.intWSID);
                }

                objIssueManage.IssueIndex(State.intWSID);

                //栏目--重新发布
                objIssueManage.IssueSection(true, State.intWSID);

                //文章--重新发布
                objIssueManage.IssueArticle(true, State.intWSID);

                objIssueManage.ClearCaches();
            }

            Alert("整站发布成功！");
        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
            //清除当前Session对象
            Session.Clear();
            //清除当前Application对象
            Application.Clear();
            //清除登陆验证票据
            System.Web.Security.FormsAuthentication.SignOut();
            AlertAndExecOther("已注销！", "正在跳转至系统登陆页，请稍等……", " window.parent.location='" + System.Web.HttpContext.Current.Request.Path.Replace("ServerPages/Top.aspx", "") + "login.aspx'");
        }
    }
}